import type {Plugin} from "vite"
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx';
import legacy from '@vitejs/plugin-legacy';
import Components from 'unplugin-vue-components/vite'
import { viteMockServe } from "vite-plugin-mock"
import viteCompression from "vite-plugin-compression"
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'

export function createVitePlugins(isBuild: boolean, env): Plugin[] {
  const plugins = [
    vue(),
    vueJsx(),
    Components({
      dirs: ['src/components'],
      extensions: ['vue'],
      deep: true,
      resolvers: [
        AntDesignVueResolver(),
      ],
    }),
  ]
  if(isBuild) {
    env.VITE_BUILD_LEAGCY === "on" && plugins.push(legacy({
      targets: ['ie >= 11'],
      additionalLegacyPolyfills: ['regenerator-runtime/runtime']
    }))
    env.VITE_BUILD_GZIP === "on" && plugins.push(viteCompression({
      deleteOriginFile: false,
       // 超过的进行压缩，默认值1025 单位为b
       threshold: 5 * 1024 + 1 // 5KB
    }))
  } else {
    env.VITE_HTTP_MOCK === "on" && plugins.push(viteMockServe({
      mockPath: "src/mock",
      supportTs: false     //如果使用 js发开，则需要配置 supportTs 为 false
    }))
  }
  return plugins
}